﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using LogisticsBLL;
using System.Text.RegularExpressions;

namespace WebLosgic.Driver
{
    public partial class UpDriver : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int DrID =Convert.ToInt32(Request.QueryString["did"]);
                if (DrID != 0)
                {
                    BindDriver(DrID);
                }
                else
                {
                    Response.Redirect("DriverXinxi.aspx");
                }
            }
        }

        public void BindDriver(int id)
        {
            DriverBLL bll = new DriverBLL();
            Drivers dr = bll.UpBindDriver(id);
            TxtName.Text = dr.Name;
            TxtPhone.Text = dr.Phone;
            TxtRemark.Text = dr.Remark;
            TxtTeamID.Text = dr.FK_TeamID.ToString();
            TxtIDcard.Text = dr.IDCard;
            TxtAddTime.Text =dr.CheckInTime.ToString();
            iptBrith.Value = dr.Birth.ToString();
            rblSex.SelectedValue = dr.Sex;
            RblState.SelectedValue = dr.State;
        }


        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            string phonereg = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
            Regex re = new Regex(phonereg);
            if (re.IsMatch(TxtPhone.Text.Trim()) == false)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('手机号码格式不正确')", true);
            }
            int DrID = Convert.ToInt32(Request.QueryString["did"]);
            DriverBLL dbl = new DriverBLL();
            Drivers dr = new Drivers();
            dr.Name=TxtName.Text;
            dr.Phone=TxtPhone.Text;
            dr.Remark=TxtRemark.Text;
            dr.FK_TeamID=Convert.ToInt32(TxtTeamID.Text);
            dr.IDCard=TxtIDcard.Text;
            dr.CheckInTime=Convert.ToDateTime(TxtAddTime.Text);
            dr.Birth = Convert.ToDateTime(iptBrith.Value);
            dr.Sex=rblSex.SelectedValue;
            dr.State=RblState.SelectedValue;
            dr.DriverID = DrID;
            int result = dbl.UpdateDriver(dr);
            if (result > 0)
            {
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 修改驾驶员信息";
                int TypeId = 13;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + TxtName.Text + "," + TxtPhone.Text + "," + TxtIDcard;
                string ProcName = "update Driver set [Name] where DriverID";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改成功')", true);
                }
                
            }
        }

        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("DriverXinxi.aspx");
        }
    }
}